<?php
/*! \brief
 * This is WFExitCode abstract class
 * This class is maintain all the exit code of php CLI via interface_request.php
 * EX : php /path/to/interface.php <<file_interface>> <= execute this CLI will return exit code.
 * Important:
 * Exit statuses should be in the range 0 to 254, 
 * the exit status 255 is reserved by PHP and shall not be used.
 *\author William Su
 */
abstract class AExitCode
{
    /**
     * @var WF
     */
    protected $objFramework;
    protected static $instance;
    
    const SUCCESS = 0;
    const ERROR_WRONG_PARAMETER = 1;
    const SHOW_HELP = 99;
    const ERROR_INTERFACE_COMMAND_INVALID = 140;
    const ERROR_INVALID_VERSION_NUMBER = 150;
    const ERROR_INVALID_FILE_INTERFACE = 160;
    const ERROR_CMD_REQUIRE_FILE_INTERFACE = 170;
    const ERROR_FILE_INTERFACE_REQUIRE_FILE_TARGET = 180;
    const ERROR_FILE_INTERFACE_REQUIRE_FILE_CONDITION = 190;
    const ERROR_FILE_INTERFACE_REQUIRE_VERSION_NUMBER = 200;
    
    public function __construct($objFramework){
        $this->objFramework = $objFramework;
        
        self::$instance['ExitCodeMsg'] = array();
        self::$instance['ExitCodeMsg'][self::SUCCESS] = 'Execute success!';
        self::$instance['ExitCodeMsg'][self::ERROR_WRONG_PARAMETER] = 'Error wrong parameter!';
        self::$instance['ExitCodeMsg'][self::SHOW_HELP] = 'Show help';
        self::$instance['ExitCodeMsg'][self::ERROR_INTERFACE_COMMAND_INVALID] = 'Error interface command invalid!';
        self::$instance['ExitCodeMsg'][self::ERROR_INVALID_VERSION_NUMBER] = 'Error invalid version number!';
        self::$instance['ExitCodeMsg'][self::ERROR_INVALID_FILE_INTERFACE] = 'Error invalid file interface!';
        self::$instance['ExitCodeMsg'][self::ERROR_CMD_REQUIRE_FILE_INTERFACE] = 'Error cmd require file_interface!';
        self::$instance['ExitCodeMsg'][self::ERROR_FILE_INTERFACE_REQUIRE_FILE_TARGET] = 'Error file_interface require file_target!';
        self::$instance['ExitCodeMsg'][self::ERROR_FILE_INTERFACE_REQUIRE_FILE_CONDITION] = 'Error file_interface require filter_condition!';
        self::$instance['ExitCodeMsg'][self::ERROR_FILE_INTERFACE_REQUIRE_VERSION_NUMBER] = 'Error file_interface require version number!';
    }
    
    public function getMsgExitCode($intExitCode){
        $ret = isset(self::$instance['ExitCodeMsg'][$intExitCode]) ? self::$instance['ExitCodeMsg'][$intExitCode] : 'ExitCodeNoFound:['.$intExitCode.']';
        return $ret;
    }
    
    /**
     *\brief Description
     * This method will get Exit code message by $file_interface
     * Expect getMsgByFileInterface() context as below
     * switch($file_interface){
     *    case 'initWidgetPoolDBSchema.php':
     *        $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpInitWidgetPoolDBSchema();
     *    break;
     *    case 'mydefault': //attention : must have this case
     *        $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpInterfaceRequest();
     *    break;
     * }
     * @param string $file_interface
     */
    abstract protected function getMsgByFileInterface($file_interface);/*
    {
        switch($file_interface){
            case 'initWidgetPoolDBSchema.php':
                $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpInitWidgetPoolDBSchema();
            break;
            case 'mydefault': //this case is required!
                $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpInterfaceRequest();
            break;
        }
        return $msg;
    }*/
    /**
     *\brief Description
     * This method will get Exit code message by $intCode
     * @param integer $file_interface
     */
    abstract protected function getMsgByCode($intCode);/*{
        $msg = '';
        switch($intCode){
            case self::ERROR_INIT_WIDGET_POOL_DB_SCHEMA:
                $msg = $this->objFramework->getWidgetFactory()->getInterface()->getMsgErrorInitWidgetPoolDbSchema();
            break;
        }
        return $msg;
    }*/ 
    /**
     *\brief Description 
     * - Via this exitCli, besides exit($exit_code), 
     * - if $exit_code is a failed exit code, will automatic show relative error information
     */
    public function exitCli($code, $file_interface = null, $msg = null){
        mydebug_log($msg);
        $file_interface = null === $file_interface ? 'mydefault' : $file_interface;
        $msg = null !== $msg ? $msg : '';
        if(!$msg){
            switch($code){
                case '0': //Success
                    $msg = '';
                break;
                case self::ERROR_WRONG_PARAMETER:
                case self::SHOW_HELP:
                    $msg = $this->getMsgByFileInterface($file_interface);
                    /*switch($file_interface){
                        case 'initWidgetPoolDBSchema.php':
                            $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpInitWidgetPoolDBSchema();
                        break;
                        case 'synWidgetXMLToDB.php':
                            $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpSynWidgetXMLToDB();
                        break;
                        case 'execDBUpdateFilter.php':
                            $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpExecDBUpdateFilter();
                        break;
                        case 'turnOnWidgetOfCategoryByVersion.php':
                            $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpTurnOnWidgetOfCategoryByVersion();
                        break;
                        case 'turnOffWidgetOfCategoryByVersion.php':
                            $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpTurnOffWidgetOfCategoryByVersion();
                        break;
                        case 'deleteWidgetFromDBByWidgetName.php':
                            $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpDeleteWidgetFromDBByWidgetName();
                        break;
                        case 'synWidgetXMLToDBByWidgetName.php':
                            $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpSynWidgetXMLToDBByWidgetName();
                        break;
                        case 'mydefault':
                            $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpInterfaceRequest();
                        break;
                    }*/
                break;
                
                case self::ERROR_INVALID_FILE_INTERFACE:
                    $msg = $this->objFramework->getWidgetFactory()->getInterface()->getMsgErrorInvalidFileInterface();
                break;
                
                
                
                case self::ERROR_CMD_REQUIRE_FILE_INTERFACE:
                    $msg = $this->objFramework->getWidgetFactory()->getInterface()->getMsgErrorCmdRequireFileInterface();
                break;
                case self::ERROR_FILE_INTERFACE_REQUIRE_FILE_TARGET:
                    $msg = $this->objFramework->getWidgetFactory()->getInterface()->getMsgErrorFileInterfaceRequireFileTarget();
                break;
                case self::ERROR_FILE_INTERFACE_REQUIRE_FILE_CONDITION:
                    $msg = $this->objFramework->getWidgetFactory()->getInterface()->getMsgErrorFileInterfaceRequireFileCondition();
                break;
                case self::ERROR_FILE_INTERFACE_REQUIRE_VERSION_NUMBER:
                    $msg = $this->objFramework->getWidgetFactory()->getInterface()->getMsgErrorFileInterfaceRequireVersionNumber();
                break;
                
                case self::ERROR_INVALID_VERSION_NUMBER:
                    $msg = $this->objFramework->getWidgetFactory()->getInterface()->getHelpInvalidVersionNumber();
                break;
                default:
                    $msg = $this->getMsgByCode($code);
                    $msg = $msg ? $msg : $this->objFramework->getWidgetFactory()->getInterface()->getHelpInterfaceRequest();
                break;
            }
        }
        if($msg){
            if(false === $this->objFramework->isUnitTest()){
                echo $msg;
            }
        }
        exit($code);
    }
}